package com.gaohe.service.imp;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaohe.mapper.AddressBookMapper;
import com.gaohe.pojo.AddressBook;
import com.gaohe.pojo.User;
import com.gaohe.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

@Service
public class AddressBookServiceImp extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {
    @Autowired
    HttpServletRequest request;

    @Override
    @Transactional
    public void up_default(AddressBook addressBook) {
        User user = (User) request.getSession().getAttribute("user");
        //先把所有的地址默认字段都改为0，然后再把需要的地址默认信息字段改为1
        LambdaUpdateWrapper<AddressBook> luw = new LambdaUpdateWrapper();
        luw.eq(AddressBook::getUserId, user.getId())
                .set(AddressBook::getIsDefault, 0);
        this.update(luw);
        LambdaUpdateWrapper<AddressBook> luw2 = new LambdaUpdateWrapper();
        luw2.eq(AddressBook::getId, addressBook.getId())
                .eq(AddressBook::getUserId, user.getId())
                .set(AddressBook::getIsDefault, 1);
        this.update(luw2);
    }


}
